<?php

/* 
 * Class teia\webdev\php\core\Page
 * Core class for creating web pages dynamically
 * 
 * @author Bernardo Quibiana
 * @date created: 20111001
 * 
 * @date modified: 20111018
 * @modified: Bernardo Quibiana
 */

// namespace declaration: Core Abstract Data Types
namespace teia\webdev\php\core;

require_once 'FunctionPool.inc';

class Page {

	protected $id;	// [String] unique page identifier
	protected $title;	// [Strign] the page title
	protected $body;	// [String] the body html content
	protected $int_style;	// [String] internal style sheet
	protected $ext_style;	// [String] path to external stylesheet
	protected $keywords;	// [String[]] array with keywords for web search [Semantic Web]

	protected $strict;	// [boolean] specify if the creation should be strict
	
	static function create_tag($element, $content, $attributes = NULL) {
		$attribStr = '';
		if ($attributes) {
			$attribStr = FunctionPool::assoc_to_str($attributes);
		}
		
		return "<$element $attribStr> $content </$element>";
	}
	
	static function display_tag($element, $content, $attributes = NULL) {
		echo self::create_tag($element, $content, $attributes = NULL);
	}
	
	public function __construct($id, $title, $body, $ext_style = NULL, $int_style = NULL, $keywords = NULL) {
		$this->id = $id;
		$this->title = $title;
		$this->body = $body;
			
		// parse
		if ($ext_style) {
			$this->ext_style = $ext_style;
		}
		if ($int_style) {
			$this->int_style = $int_style;
		}	
		if ($keywords && is_array($keywords)) {
			if(is_array($keywords)) {
				$this->keywords = $keywords;
			} else {
				array_push($this->keywords, $keywords);
			}
		}
	}	// end of __construct

	public function __get($name) {
		return $this->$name;
	}
	
	public function __set($name, $value) {
		$this->$name = $value;
	}

	public function create_title_tag() {
		return self::create_tag('title', $this->title);
	}
	
	public function display_title() {
		echo $this->create_title_tag();
	}

	public function create_int_style_tag() {
		return self::create_tag('style', $this->int_style);
	}
	
	public function display_int_style() {
		echo $this->create_int_style_tag();
	}

	public function create_ext_style_tag() {
		return "<link rel=\"stylesheet\" href=\"$this->ext_style\" />";
	}
	
	public function display_ext_style() {
		echo $this->create_ext_style_tag();
	}

	public function create_keywords_tag() {
		$keywords_str = self::array_to_str($this->keywords, ', ');
		return "<meta name=\"keywords\" content=\"$keywords_str\" />\n";
	}
	
	public function display_keywords() {
		echo $this->create_keywords_tag();
	}

	public function create_head_tag() {
		$title = $this->create_title_tag();
		$ext_style = $this->create_ext_style_tag();
		$int_style = $this->create_int_style_tag();
		$keywords = $this->create_keywords_tag();
		// <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
			
		return self::create_tag("head", "\n" . $title . "\n" . $ext_style . "\n" . $int_style . "\n" . $keywords . "\n");
	}
	
	public function display_head() {
		echo $this->create_head_tag();
	}

	public function create_body_tag() {
		return self::create_tag("body", "\n" . $this->body . "\n");
	}
	
	public function display_body() {
		echo $this->create_body_tag();
	}

	public function create_document() {
		$head = $this->create_head_tag();
		$body = $this->create_body_tag();
		$html = self::create_tag("html", "\n" . $head . "\n" . $body . "\n");
			
		return "<!DOCTYPE HTML>\n" . $html;
	}
	
	public function display() {
		echo $this->create_document();
	}

	public function add_content($content) {
		$this->body .= $content;
	}

	public function add_keyword($keyword) {
		array_push($this->keywords, $keyword);
	}

	public function search_keywords($target) {
			
	}

	public function search_content($target) {
			
	}

	public function search($target) {
			
	}
	
	public function save_to_file($path, $append = FALSE) {
		$rc = 0;
		$mode = $append ? 'a' : 'w';	// append or write
		$handle = fopen($path, $mode);
		$doc = $this->create_document();	// generate the html document
		
		if ($handle) {
			flock($handle, LOCK_EX);	// lock file for writing
			$rc = fwrite($handle, $doc);	// write data to file
			flock($handle, LOCK_UN);	// unlock file
			fclose($handle);	// close file
		}
		
		return $rc;
	}
}	// end of class Page

?>